package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("strategies")
public class StrategyController {
    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @Autowired
    private IStrategyCommentService strategyCommentService;
    @PostMapping("/addComment")
    @RequireLogin
    public Object addComment(StrategyComment comment, @UserParam UserInfo userInfo) {
        strategyCommentService.addComment(comment, userInfo);
        strategyStatisVORedisService.queryReplynum(comment.getStrategyId());
        return JsonResult.success();
    }

    @GetMapping("/detail")
    public Object detail(String id) {
        strategyStatisVORedisService.queryViewNum(id);
        return JsonResult.success(strategyService.get(id));
    }

    @GetMapping("/query")
    public Object query(StrategyQuery qo) {
        return JsonResult.success(strategyService.query(qo));
    }
    @GetMapping("/themes")
    public Object themes() {
        return JsonResult.success(strategyThemeService.list());
    }
    @GetMapping("/comments")
    public Object comments(StrategyCommentQuery qo) {
        Page<StrategyComment> page = strategyCommentService.query(qo);
        return  JsonResult.success(page);
    }
    @GetMapping("/statisVo")
    public Object statisVo(String sid) {
        return JsonResult.success(strategyStatisVORedisService.find(sid));
    }

    @PostMapping("/commentThumb")
    @RequireLogin
    public Object commentThumb(String cid, String sid, @UserParam UserInfo userInfo){
        String userInfoId = userInfo.getId();
        strategyCommentService.commentThumb(cid, sid, userInfoId);
        return JsonResult.success();
    }
    @PostMapping("/favor")
    @RequireLogin
    public Object favor(String sid, @UserParam UserInfo userInfo){
        String uid = userInfo.getId();
        boolean ret = strategyStatisVORedisService.queryFavornum(sid,uid);
        return JsonResult.success(ret);
    }
    @PostMapping("/strategyThumbup")
    @RequireLogin
    public Object strategyThumbup(String sid, @UserParam UserInfo userInfo){
        String uid = userInfo.getId();
        boolean ret = strategyStatisVORedisService.queryStrategyThumbup(sid, uid);

        return JsonResult.success(ret);
    }
}


